﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class F530 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);
        if (v_int_donviID == 0) Response.Redirect("F500.aspx");
        if (!IsPostBack)
        {
            LoadNganh();
            BindData();
        }
    }

    private void LoadNganh()
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);
        cboMonHoc.DataSource = Services.GetcboNganhByDonViId(v_int_donviID);
        cboMonHoc.DataTextField = "TEN_NGANH";
        cboMonHoc.DataValueField = "ID";
        cboMonHoc.DataBind();
    }



    private void BindData()
    {
        try
        {
            DM_DON_VI_SU_DUNG v_obj_DM_DON_VI_SU_DUNG = new DM_DON_VI_SU_DUNG();
            m_lbl_thong_bao.Text = "Không có thông báo gì.";

            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);



            string v_str_info = "####";
            if ((v_int_donviID != 0))
            {
                v_obj_DM_DON_VI_SU_DUNG = Services.GetDM_DON_VI_SU_DUNGById(v_int_donviID);
                v_str_info = (!String.IsNullOrEmpty(v_obj_DM_DON_VI_SU_DUNG.TEN_DON_VI) ? v_obj_DM_DON_VI_SU_DUNG.TEN_DON_VI : "######") ;
                this.lblMonHocInfo.Text = v_str_info + " -> Danh sách ngành. ";
            }

            this.lblF221.Text = v_str_info + " ->  F530 - Thêm mới ngành học";

            List<DM_NGANH_DAO_TAO> lst = Services.GetDsDM_NGANH_DAO_TAOByDonViId(v_int_donviID);

            if (lst != null)
            {
                gvDsMonHoc.DataSource = lst;
                gvDsMonHoc.DataBind();
                // Set event for Delete button
                foreach (GridViewRow row in gvDsMonHoc.Rows)
                {
                    ImageButton imageButton = (ImageButton)row.FindControl("imgBtDelete");
                    imageButton.Attributes.Add("OnClick", "javascript:return confirm('Bạn có chắc chắn muốn xóa ngành học này?')");
                }
            }
            else
            {

            }

        }
        catch (Exception)
        {

        }

    }

    protected void gvDsMonHoc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName.ToUpper())
        {
            case "DEL":
                bool isok = Services.DeleteDM_DON_VI_NGANH(id);
                if (isok)
                {
                    Response.Redirect(String.Format("F530.aspx?donviId={0}", v_int_donviID));
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, xóa không thành công!";
                    m_lbl_mess.Visible = true;
                }
                break;
        }
    }

    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);

            int v_int_NganhId = 0;
            int.TryParse(this.cboMonHoc.SelectedValue, out v_int_NganhId);

            if (v_int_donviID != 0 && v_int_NganhId != 0)
            {
                DM_DON_VI_NGANH donvinganh = new DM_DON_VI_NGANH();
                donvinganh.ID_DON_VI_SU_DUNG = v_int_donviID;
                donvinganh.ID_NGANH = v_int_NganhId;
                bool isOk = Services.InsertDM_DON_VI_NGANH(donvinganh);
                if (isOk)
                {
                    Response.Redirect(String.Format("F530.aspx?donviId={0}", v_int_donviID));
                }
            }
            else
            {
                this.m_lbl_mess.Text = "Bạn chưa chọn ngành học!";
                this.m_lbl_mess.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
        }

    }
    protected void gvDsMonHoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDsMonHoc.PageIndex = e.NewPageIndex;
        BindData();
    }
}